<?php


namespace App\Http\Controllers\App;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Models\MainDB\Gift;
use App\Repositories\Lottery\DrawRecordRepository;
use App\Repositories\Lottery\PrizeRepository;
use App\Repositories\Lottery\RankRepository;
use App\Repositories\Lottery\SettingRepository;
use App\Repositories\Lottery\UserRepository;
use App\Resource\User\BalanceResource;
use App\Response\Response;
use Illuminate\Http\Request;
use App\Repositories\Lottery\LotteryRepository as Repository;
use App\Resource\Draw\Resource;
use App\Resource\Prize\Resource as PrizeResource;
use App\Resource\Setting\Resource as SettingResource;
use App\Resource\Rank\LuckyCountResource;

class LotteryController extends BaseController
{
    public function balance(Request $request)
    {
        $userId = (int)UID;
        $balance = UserRepository::getInstance()->info($userId);
        $model = BalanceResource::make($balance);
        return Response::sendData($model);
    }

    public function setting(Request $request)
    {
        $setting = SettingRepository::getInstance()->one([]);
        $model = SettingResource::make($setting);
        return Response::sendData($model);
    }

    public function rank(Request $request)
    {
        $userId = (int)UID;
        $type = $filters['type'] ?? '';
        $perPage = (int)$request->query('per_page', 10);
        $models = RankRepository::getInstance()->luckyRank($userId, $type, $perPage);
        return Response::sendData(LuckyCountResource::make($models));
    }

    public function prizes(Request $request)
    {
        $models = PrizeRepository::getInstance()->show();
        return Response::sendData(PrizeResource::collection($models));
    }

    public function swap(Request $request)
    {
        $userId = (int)UID;
        $count = intval($request->input('count'));
        if ($count <= 0) {
            throw new BasicException(-1, '非法请求');
        }
        UserRepository::getInstance()->swap($userId, $count);
        return Response::sendData([]);
    }

    public function draw(Request $request)
    {
        $userId = (int)UID;
        $count = intval($request->input('count'));
        if ($count <= 0 || $count > 100) {
            throw new BasicException(-1, '非法请求');
        }
        $plateId = intval($request->input('plate_id'));
        $draw = Repository::getInstance()->draw($userId, $count, $plateId);
        $models = DrawRecordRepository::getInstance()->get(['draw_id' => $draw->id])->sortByDesc('prize_price');
        return Response::sendData(Resource::collection($models));
    }

    public function clearPoolPrize(Request $request)
    {
        $drawType = $request->input('drawType');
        $tag = $request->input('tag');
        if (empty($drawType) || empty($tag)) {
            throw new BasicException(-1, '非法请求,参数缺失');
        }
        Repository::getInstance()->clearPoolPrize($drawType, $tag);
        return Response::sendData([]);
    }

    public function plutocrat(Request $request)
    {
        $type = $request->input('type');  // 1 2块 2 5块  3 10块 4 20块
        if (empty($type)) {
            throw new BasicException(-1, '非法请求,参数缺失');
        }
        switch ($type) {
            case "2":
                $key = "RICH_AWARD_FIVE";
                break;
            case "3":
                $key = "RICH_AWARD_TEN";
                break;
            case "4":
                $key = "RICH_AWARD_TWENTY";
                break;
            default:
                $key = "RICH_AWARD_TWO";
        }
        $gift = app('model_config')->getValue($key);
        if (empty($gift)) return Response::sendData([]);
        $giftIds = explode(',', $gift);
        $data = Gift::query()->whereIn("id", $giftIds)->orderByDesc('price')->select(['name', 'picture', 'price'])->get()->toArray() ?? [];
        return Response::sendData($data);
    }
}
